package fr.cephb.socialsnp.core;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class SnpSocialModel
	{
	
	private  EntityManager entityManager;
	
	public SnpSocialModel(EntityManager entityManager)
		{
		this.entityManager=entityManager;
		}
	
	public EntityManager getEntityManager() {
		return entityManager;
		}
	
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			final  EntityManagerFactory emf=Persistence.createEntityManagerFactory("TopLinkPersistenceUnit1");
			
			SnpSocialModel app= new SnpSocialModel(emf.createEntityManager());
			
			User user=app.entityManager.find(User.class, 1L);
			System.err.println(user.getRole());
			Query query=app.entityManager.createNamedQuery("user.byLogin");
			query.setParameter("login", "root");
			user=(User)query.getSingleResult();
			System.err.println(user.getRole());
			
			 query=app.entityManager.createNamedQuery("datasetAll");
			 System.err.println(query.getResultList());
			 System.out.println("OK");
			 
			
			 
			query=app.entityManager.createQuery("select C from User2Project C");
			 System.err.println(query.getResultList());
			 
			 query=app.entityManager.createQuery("select C from User2Project C,Project P where C.project=P and C.role= :role");
			 query.setParameter("role",Role.ADMIN);
			 for(Object o:query.getResultList())
			 	{
				User2Project u2p= (User2Project)o;
				for(User p:u2p.getProject().getUsers())
					{
					System.err.println(" >"+p);
					}
			 	}
			
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
